<?php

namespace backend\models;

use yii\base\Model;

class OperForm extends Model {
	//属性
	public $RoleID;//角色id
	public $username;//操作员姓名
	public $Pwd;//密码
	public $Repwd;//确认密码
	public $Address;//地址
	public $LinkTel;//联系电话
	public $QQ;//qq
	public $Email;//电子邮箱
	public $Mobile;//手机
	public $SortID;//排序编码
	public $State;//状态
	public $remember;//记住密码
	public $verifycode;//验证码

	
	//规则
	public function rules(){
		return [
			//添加操作员部分
			[['RoleID','username','Pwd','Repwd','LinkTel','Mobile'],'required','message'=>'{attribute}不能为空','on'=>'addoperator'],
			['RoleID','exist','targetClass'=>TauRoleinfo::className(),'targetAttribute'=>'ID','message'=>'所选角色不存在','on'=>'addoperator'],
			['Pwd','string','min'=>6,'max'=>15,'tooShort'=>'密码最少6位','tooLong'=>'密码最多15位','on'=>'addoperator'],
			['Repwd','compare','compareAttribute'=>'Pwd','message'=>'密码不匹配','on'=>'addoperator'],
			[['LinkTel','Mobile'],'match','pattern'=>'/(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/','message'=>'电话格式不正确','on'=>'addoperator'],
			['QQ','match','pattern'=>'/[1-9][0-9]{4,}/','message'=>'QQ格式不正确','on'=>'addoperator'],
			['Email','email','message'=>'邮箱格式不正确','on'=>'addoperator'],
			['SortID','integer','integerOnly'=>true,'message'=>'排序编码必须为整型','on'=>'addoperator'],
			//登陆部分
			[['username','Pwd','verifycode'],'required','message'=>'{attribute}不能为空','on'=>'login'],
			['Pwd','validatePassword','on'=>'login'],
			['verifycode','captcha','captchaAction'=>'main/captcha','message'=>'验证码错误','on'=>'login'],
			//操作员信息修改
			[['RoleID','username','Pwd','LinkTel','Mobile'],'required','message'=>'{attribute}不能为空','on'=>'update'],
			['RoleID','exist','targetClass'=>TauRoleinfo::className(),'targetAttribute'=>'ID','message'=>'所选角色不存在','on'=>'update'],
			['Pwd','string','min'=>6,'max'=>15,'tooShort'=>'密码最少6位','tooLong'=>'密码最多15位','on'=>'update'],
			[['LinkTel','Mobile'],'match','pattern'=>'/(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/','message'=>'电话格式不正确','on'=>'update'],
			['QQ','match','pattern'=>'/[1-9][0-9]{4,}/','message'=>'QQ格式不正确','on'=>'update'],
			['Email','email','message'=>'邮箱格式不正确','on'=>'update'],
			['SortID','integer','integerOnly'=>true,'message'=>'排序编码必须为整型','on'=>'update'],
		];
	}
	
	//场景
	public function scenarios(){
		return [
			'addoperator'=>['RoleID','username','Pwd','Repwd','Address','LinkTel','QQ','Email','Mobile','SortID','State'],
			'login'=>['username','Pwd','verifycode','remember'],
			'update'=>['RoleID','username','Pwd','Address','LinkTel','QQ','Email','Mobile','SortID','State'],
		];
	}
	
	//属性标签
	public function attributeLabels(){
		return [
			'RoleID'=>'角色',
			'username'=>'操作员姓名',
			'Pwd'=>'密码',
			'Repwd'=>'确认密码',
			'Address'=>'地址',
			'LinkTel'=>'联系电话',
			'QQ'=>'QQ',
			'Email'=>'电子邮箱',
			'Mobile'=>'手机号',
			'SortID'=>'排序编码',
			'State'=>'状态',
			'remember'=>'记住密码',
			'verifycode'=>'验证码'
		];
	}
	
	//自定义验证规则，验证密码
	public function validatePassword($attribute,$params){
		if(!$this->hasErrors()){
			$operator=TauOperinfo::findOne(['username'=>$this->username]);
			//判断用户名是否正确
			if(!$operator){
				return $this->addError($attribute,'操作员不存在');
			}
			//判断密码是否正确
			
			if($operator->Pwd != substr(md5(trim($this->Pwd.$operator->auth_key)), 9,12)){
				return $this->addError($attribute,'密码错误');
			}
		}
	}
	
	//登陆
	public function login(){
		$operator=TauOperinfo::findOne(['username'=>$this->username]);
		
		$lifetime=($this->remember)?3600*24:0;
		if(\Yii::$app->user->login($operator,$lifetime)){
			
			return true;
		}else{
			return false;
		}
	}
	
	
}

?>